#                            第十五节 数据可视化

# 1、简单的折线图
import matplotlib.pyplot as plt

lb = [1, 4, 9, 25]
fig, ax = plt.subplots()
ax.plot(lb)

plt.show()

# 1、修改折线图的样式

import matplotlib.pyplot as plt

lb = [1, 4, 9, 25]
fig, ax = plt.subplots()
ax.plot(lb, linewidth=3)
#设置图表表标题，并给坐标轴加上标签
ax.set_title("平方数折线图",fontsize=24)
ax.set_xlabel("横坐标：值",fontsize=14)
ax.set_ylabel("纵坐标：值的平方",fontsize=14)

#设置刻度标记的大小
ax.tick_params(axis='both', labelsize=14)

#显示中文
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.show()

# 3、校正图形
import matplotlib.pyplot as plt
lb = [1,2,3,4,5]
lb2 = [1,4,9,16,25]
fig, ax = plt.subplots()
ax.plot(lb, lb2, linewidth = 3)
plt.show()

# 4、使用内置样式
import  matplotlib.pyplot as plt
print(plt.style.available)

lb = [1,2,3,4,5]
lb2 = [1,4,9,16,25]

plt.style.use('seaborn')

fig, ax = plt.subplots()
ax.plot(lb, lb2, linewidth = 3)
plt.show()
# 5、使用scatter()绘制散点图并设置样式
import matplotlib.pyplot as plt
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(2,4)
plt.show()
# 5.2 使用scatter() 绘制一系列点
import matplotlib.pyplot as plt

x_values = [1, 2, 4, 5]
y_values = [1, 4, 14, 25]

plt.style.use('seaborn')
fig, ax = plt.subplots()

ax.set_title('平方数', fontsize = 20)
ax.set_xlabel('x轴',fontsize = 13)
ax.set_ylabel('y轴', fontsize = 14)
ax.tick_params(axis = 'both', which = 'major', labelsize = 14)

ax.scatter(x_values, y_values, s = 100)

#显示中文
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.show()

# 6、自动计算数据
import matplotlib.pyplot as plt

x_values = range(1,1001)
y_values = [x**2 for x in x_values]

fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=10)

ax.set_title('自动计算平方值', fontsize = 21)
#设置每个坐标的取值范围
ax.axis([0, 1100, 0, 1100000])

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.show()

# 7、自定义颜色
import matplotlib.pyplot as plt
x = ['一月', '二月', '三月']
y = ['3000', '2000', '4000']
plt.style.use('seaborn')
fig, ax = plt.subplots()

ax.set_title('自定义颜色', fontsize=15)
ax.set_xlabel('x轴',fontsize=13)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.scatter(x, y, c='red', s=10)
#显示中文
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.show()

# 8、设置图表标题和坐标
import matplotlib.pyplot as plt
x1 = range(1,1001)
y1 = [x**2 for x in x1]
fig, ax = plt.subplots()
ax.set_title('使用颜色映射', fontsize=14)
ax.set_xlabel('x轴', fontsize=13)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
ax.scatter(x1, y1, c=y1, cmap=plt.cm.Blues, s=10)
plt.show()

#9、自动保存图片
import matplotlib.pyplot as plt
x1 = range(1,1001)
y1 = [x**2 for x in x1]
fig, ax = plt.subplots()
ax.set_title('自动保存图片', fontsize=14)
ax.set_xlabel('x轴', fontsize=13)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
ax.scatter(x1, y1, c=y1, cmap=plt.cm.Blues, s=10)
plt.savefig('date_view9.png')
